<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Linux下安装MongoDB服务 | 星辰阁</title>
  <meta name="keywords" content=" 软件安装 , MongoDB ">
  <meta name="description" content="Linux下安装MongoDB服务 | 星辰阁">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="description" content="个人简介李瑞桐 毕业于东北石油大学，目前从事殡葬相关应用开发工作。 喜欢研究新兴技术。 联系方式QQ :86415270邮箱 :86415270@qq.com">
<meta property="og:type" content="website">
<meta property="og:title" content="about">
<meta property="og:url" content="https://loveyangxiaoyu.gitee.io/lrt_blog/about/index.html">
<meta property="og:site_name" content="星辰阁">
<meta property="og:description" content="个人简介李瑞桐 毕业于东北石油大学，目前从事殡葬相关应用开发工作。 喜欢研究新兴技术。 联系方式QQ :86415270邮箱 :86415270@qq.com">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-11-11T07:11:13.000Z">
<meta property="article:modified_time" content="2022-11-20T05:27:08.036Z">
<meta property="article:author" content="李瑞桐">
<meta name="twitter:card" content="summary">


<link rel="icon" href="/lrt_blog/img/favicon.png" type="images/x-icon">

<link href="/lrt_blog/css/style.css?v=1.0.0" rel="stylesheet">

<link href="/lrt_blog/css/hl_theme/atom-light.css?v=1.0.0" rel="stylesheet">

<link href="//cdn.bootcss.com/animate.css/3.5.2/animate.min.css" rel="stylesheet">
<link href="//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="/lrt_blog/js/jquery.autocomplete.min.js?v=1.0.0" ></script>

<script src="//cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js"></script>
<script>
    hljs.initHighlightingOnLoad();
</script>

<script src="//cdn.bootcss.com/nprogress/0.2.0/nprogress.min.js"></script>



<script src="//cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js" ></script>

<script src="/lrt_blog/js/iconfont.js?v=1.0.0" ></script>

<meta name="generator" content="Hexo 4.2.1"><link rel="alternate" href="/lrt_blog/atom.xml" title="星辰阁" type="application/atom+xml">
</head>
<div style="display: none">
  <input class="theme_disqus_on" value="false">
  <input class="theme_preload_comment" value="false">
  <input class="theme_blog_path" value="/lrt_blog">
</div>

<body>
<aside class="nav">
    <div class="nav-left">
        <a href="/lrt_blog/" class="avatar_target">
    <img class="avatar" src="/lrt_blog/img/avatar.jpg" />
</a>
<div class="author">
    <span>李瑞桐</span>
</div>

<div class="icon">
    
        
        <a title="rss" href="/lrt_blog/atom.xml" target="_blank">
            
                <svg class="iconfont-svg" aria-hidden="true">
                    <use xlink:href="#icon-rss"></use>
                </svg>
            
        </a>
        
    
        
        <a title="github" href="https://github.com/lrt890424" target="_blank">
            
                <svg class="iconfont-svg" aria-hidden="true">
                    <use xlink:href="#icon-github"></use>
                </svg>
            
        </a>
        
    
        
        <a title="gitee" href="https://gitee.com/loveyangxiaoyu" target="_blank">
            
                <svg class="iconfont-svg" aria-hidden="true">
                    <use xlink:href="#icon-gitee"></use>
                </svg>
            
        </a>
        
    
        
    
        
    
        
        <a title="csdn" href="https://blog.csdn.net/lrt890424" target="_blank">
            
                <svg class="iconfont-svg" aria-hidden="true">
                    <use xlink:href="#icon-csdn"></use>
                </svg>
            
        </a>
        
    
        
        <a title="oschina" href="https://my.oschina.net/loveyangxiaoyu" target="_blank">
            
                <svg class="iconfont-svg" aria-hidden="true">
                    <use xlink:href="#icon-oschina"></use>
                </svg>
            
        </a>
        
    
        
    
</div>




<ul>
    <li><div class="all active">全部文章<small>(33)</small></div></li>
    
        
            
            <li><div data-rel="Linux"><i class="fold iconfont icon-right"></i>Linux<small>(9)</small></div>
                
                    <ul class="sub hide">
                        
                        <li><div data-rel="CentOS">CentOS<small>(3)</small></div>
                            
                        </li>
                            
                        <li><div data-rel="Ubuntu">Ubuntu<small>(2)</small></div>
                            
                        </li>
                            
                    </ul>
                
            </li>
            
        
    
        
            
        
    
        
            
        
    
        
            
            <li><div data-rel="开发工具"><i class="fold iconfont icon-right"></i>开发工具<small>(6)</small></div>
                
                    <ul class="sub hide">
                        
                        <li><div data-rel="SVN">SVN<small>(1)</small></div>
                            
                        </li>
                            
                        <li><div data-rel="Git">Git<small>(2)</small></div>
                            
                        </li>
                            
                        <li><div data-rel="jd-gui">jd-gui<small>(1)</small></div>
                            
                        </li>
                            
                        <li><div data-rel="Markdown">Markdown<small>(1)</small></div>
                            
                        </li>
                            
                        <li><div data-rel="Maven">Maven<small>(1)</small></div>
                            
                        </li>
                            
                    </ul>
                
            </li>
            
        
    
        
            
            <li><div data-rel="存储"><i class="fold iconfont icon-right"></i>存储<small>(7)</small></div>
                
                    <ul class="sub hide">
                        
                        <li><div data-rel="MongoDB">MongoDB<small>(1)</small></div>
                            
                        </li>
                            
                        <li><div data-rel="MySQL">MySQL<small>(3)</small></div>
                            
                        </li>
                            
                        <li><div data-rel="Redis">Redis<small>(1)</small></div>
                            
                        </li>
                            
                        <li><div data-rel="Navicat">Navicat<small>(1)</small></div>
                            
                        </li>
                            
                        <li><div data-rel="DM">DM<small>(1)</small></div>
                            
                        </li>
                            
                    </ul>
                
            </li>
            
        
    
        
            
            <li><div data-rel="Windows">Windows<small>(3)</small></div>
                
            </li>
            
        
    
        
            
        
    
        
            
            <li><div data-rel="笔记">笔记<small>(1)</small></div>
                
            </li>
            
        
    
        
            
        
    
        
            
            <li><div data-rel="Web服务器"><i class="fold iconfont icon-right"></i>Web服务器<small>(1)</small></div>
                
                    <ul class="sub hide">
                        
                        <li><div data-rel="Nginx">Nginx<small>(1)</small></div>
                            
                        </li>
                            
                    </ul>
                
            </li>
            
        
    
        
            
            <li><div data-rel="容器"><i class="fold iconfont icon-right"></i>容器<small>(1)</small></div>
                
                    <ul class="sub hide">
                        
                        <li><div data-rel="Docker">Docker<small>(1)</small></div>
                            
                        </li>
                            
                    </ul>
                
            </li>
            
        
    
        
            
            <li><div data-rel="前端开发"><i class="fold iconfont icon-right"></i>前端开发<small>(3)</small></div>
                
                    <ul class="sub hide">
                        
                        <li><div data-rel="Vue CLI">Vue CLI<small>(1)</small></div>
                            
                        </li>
                            
                        <li><div data-rel="Yarn">Yarn<small>(1)</small></div>
                            
                        </li>
                            
                        <li><div data-rel="Eslint">Eslint<small>(1)</small></div>
                            
                        </li>
                            
                    </ul>
                
            </li>
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
            <li><div data-rel="工具"><i class="fold iconfont icon-right"></i>工具<small>(1)</small></div>
                
                    <ul class="sub hide">
                        
                        <li><div data-rel="hexo">hexo<small>(1)</small></div>
                            
                        </li>
                            
                    </ul>
                
            </li>
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
            <li><div data-rel="开源">开源<small>(1)</small></div>
                
            </li>
            
        
    
        
            
        
    
</ul>
<div class="left-bottom">
    <div class="menus">
    
    
    
    
    </div>
    <div><a class="about  site_url"  href="/lrt_blog/about">关于</a></div>
</div>
<input type="hidden" id="yelog_site_posts_number" value="33">
<input type="hidden" id="yelog_site_word_count" value="52k">
<div style="display: none">
    <span id="busuanzi_value_site_uv"></span>
    <span id="busuanzi_value_site_pv"></span>
</div>

    </div>
    <div class="nav-right">
        <div class="friends-area">
    <div class="friends-title">
        友情链接
        <i class="back-title-list"></i>
    </div>
    <div class="friends-content">
        <ul>
            
            <li><a target="_blank" href="http://yelog.org/">叶落阁</a></li>
            
        </ul>
    </div>
</div>
        <div class="title-list">
    <form onkeydown="if(event.keyCode==13){return false;}">
        <input class="search" type="text" placeholder="以 in: 开头进行全文搜索" autocomplete="off"id="local-search-input" >
        <i class="cross"></i>
        <span>
            <label for="tagswitch">Tags:</label>
            <input id="tagswitch" type="checkbox" style="display: none" />
            <i id="tagsWitchIcon"></i>
        </span>
    </form>
    <div class="tags-list">
    
    <li class="article-tag-list-item">
        <a class="color3">教程</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color2">centos</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color1">linux</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">问题集锦</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color4">防火墙</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">软件安装</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color2">ubuntu</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color4">svn</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color4">git</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color3">MongoDB</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color2">jd-gui</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">java</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color4">反编译</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color1">MySQL</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color1">Redis</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">学习笔记</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color3">navicat</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color3">DM</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color3">windows</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">系统优化</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color3">书签</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color1">Nginx</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color2">docker</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color4">vue</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">Yarn</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">前端开发</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color2">vscode</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color2">eslint</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color4">prettier</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color4">Markdown</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">hexo</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color3">开源</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color1">Maven</a>
    </li>
    
    <div class="clearfix"></div>
</div>

    
    <div id="local-search-result">

    </div>
    
    <nav id="title-list-nav">
        
        <a  class="Windows "
           href="/lrt_blog/2022/11/20/01Windows/Windows11%E4%BC%98%E5%8C%96%E9%85%8D%E7%BD%AE/"
           data-tag="windows,系统优化"
           data-author="" >
            <span class="post-title" title="Windows11优化配置">Windows11优化配置</span>
            <span class="post-date" title="2022-11-20 00:00:00">2022/11/20</span>
        </a>
        
        <a  class="存储 DM "
           href="/lrt_blog/2021/11/16/04%E5%AD%98%E5%82%A8/%E9%93%B6%E6%B2%B3%E9%BA%92%E9%BA%9F%E5%AE%89%E8%A3%85%E8%BE%BE%E6%A2%A6%E6%95%B0%E6%8D%AE%E5%BA%93/"
           data-tag="软件安装,DM"
           data-author="" >
            <span class="post-title" title="银河麒麟安装达梦数据库">银河麒麟安装达梦数据库</span>
            <span class="post-date" title="2021-11-16 00:00:00">2021/11/16</span>
        </a>
        
        <a  class="前端开发 Vue CLI "
           href="/lrt_blog/2021/02/02/66%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91/vue-cli%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE/"
           data-tag="教程,vue"
           data-author="" >
            <span class="post-title" title="Vue CLI创建项目">Vue CLI创建项目</span>
            <span class="post-date" title="2021-02-02 21:08:20">2021/02/02</span>
        </a>
        
        <a  class="前端开发 Eslint "
           href="/lrt_blog/2021/02/01/66%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91/vscode%E4%B8%8A%E9%85%8D%E7%BD%AEEslint-Prettier/"
           data-tag="前端开发,vscode,eslint,prettier"
           data-author="" >
            <span class="post-title" title="vscode上配置Eslint+Prettier">vscode上配置Eslint+Prettier</span>
            <span class="post-date" title="2021-02-01 00:00:00">2021/02/01</span>
        </a>
        
        <a  class="前端开发 Yarn "
           href="/lrt_blog/2021/01/31/66%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91/%E5%8C%85%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B7yarn%E5%AE%89%E8%A3%85%E5%92%8C%E9%85%8D%E7%BD%AE/"
           data-tag="教程,Yarn"
           data-author="" >
            <span class="post-title" title="包管理工具yarn安装和配置">包管理工具yarn安装和配置</span>
            <span class="post-date" title="2021-01-31 17:37:05">2021/01/31</span>
        </a>
        
        <a  class="Web服务器 Nginx "
           href="/lrt_blog/2020/04/30/05web%E6%9C%8D%E5%8A%A1%E5%99%A8/Linux%E4%B8%8B%E5%AE%89%E8%A3%85Nginx%E6%9C%8D%E5%8A%A1/"
           data-tag="软件安装,Nginx"
           data-author="" >
            <span class="post-title" title="Linux下安装Nginx服务">Linux下安装Nginx服务</span>
            <span class="post-date" title="2020-04-30 10:10:58">2020/04/30</span>
        </a>
        
        <a  class="存储 MongoDB "
           href="/lrt_blog/2020/04/30/04%E5%AD%98%E5%82%A8/Linux%E4%B8%8B%E5%AE%89%E8%A3%85MongoDB%E6%9C%8D%E5%8A%A1/"
           data-tag="软件安装,MongoDB"
           data-author="" >
            <span class="post-title" title="Linux下安装MongoDB服务">Linux下安装MongoDB服务</span>
            <span class="post-date" title="2020-04-30 00:00:00">2020/04/30</span>
        </a>
        
        <a  class="存储 Redis "
           href="/lrt_blog/2020/04/30/04%E5%AD%98%E5%82%A8/Linux%E4%B8%8B%E5%AE%89%E8%A3%85Redis%E6%9C%8D%E5%8A%A1/"
           data-tag="软件安装,Redis"
           data-author="" >
            <span class="post-title" title="Linux下安装Redis服务">Linux下安装Redis服务</span>
            <span class="post-date" title="2020-04-30 00:00:00">2020/04/30</span>
        </a>
        
        <a  class="容器 Docker "
           href="/lrt_blog/2020/01/04/03%E5%AE%B9%E5%99%A8/docker%E7%A6%BB%E7%BA%BF%E5%AE%89%E8%A3%85/"
           data-tag="软件安装,docker"
           data-author="" >
            <span class="post-title" title="docker离线安装">docker离线安装</span>
            <span class="post-date" title="2020-01-04 00:00:00">2020/01/04</span>
        </a>
        
        <a  class="笔记 "
           href="/lrt_blog/2019/12/26/07Java/%E5%AD%A6%E4%B9%A0-%E4%B9%A6%E7%AD%BE/"
           data-tag="书签"
           data-author="" >
            <span class="post-title" title="学习-书签">学习-书签</span>
            <span class="post-date" title="2019-12-26 00:00:00">2019/12/26</span>
        </a>
        
        <a  class="开源 "
           href="/lrt_blog/2019/12/19/77%E5%85%B6%E4%BB%96%E5%B7%A5%E5%85%B7/%E5%9B%BD%E5%86%85%E5%BC%80%E6%BA%90%E9%95%9C%E5%83%8F%E7%AB%99%E7%82%B9/"
           data-tag="开源"
           data-author="" >
            <span class="post-title" title="国内开源镜像站点">国内开源镜像站点</span>
            <span class="post-date" title="2019-12-19 00:00:00">2019/12/19</span>
        </a>
        
        <a  class="存储 MySQL "
           href="/lrt_blog/2019/12/16/04%E5%AD%98%E5%82%A8/MySQL%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"
           data-tag="MySQL,学习笔记"
           data-author="" >
            <span class="post-title" title="MySQL 学习笔记">MySQL 学习笔记</span>
            <span class="post-date" title="2019-12-16 00:00:00">2019/12/16</span>
        </a>
        
        <a  class="存储 MySQL "
           href="/lrt_blog/2019/12/15/04%E5%AD%98%E5%82%A8/Linux%E7%8E%AF%E5%A2%83%E4%B8%8B%E5%AE%89%E8%A3%85MySQL%E6%9C%8D%E5%8A%A1/"
           data-tag="软件安装,MySQL"
           data-author="" >
            <span class="post-title" title="Linux环境下安装MySQL服务">Linux环境下安装MySQL服务</span>
            <span class="post-date" title="2019-12-15 00:00:00">2019/12/15</span>
        </a>
        
        <a  class="存储 Navicat "
           href="/lrt_blog/2019/12/15/04%E5%AD%98%E5%82%A8/Navicat%20Premium%2012%20%E7%A0%B4%E8%A7%A3/"
           data-tag="软件安装,navicat"
           data-author="" >
            <span class="post-title" title="Navicat Premium 12 破解">Navicat Premium 12 破解</span>
            <span class="post-date" title="2019-12-15 00:00:00">2019/12/15</span>
        </a>
        
        <a  class="存储 MySQL "
           href="/lrt_blog/2019/12/15/04%E5%AD%98%E5%82%A8/Windows%E7%8E%AF%E5%A2%83%E4%B8%8B%E5%AE%89%E8%A3%85MySQL%E6%9C%8D%E5%8A%A1/"
           data-tag="软件安装,MySQL"
           data-author="" >
            <span class="post-title" title="Windows环境下安装MySQL服务">Windows环境下安装MySQL服务</span>
            <span class="post-date" title="2019-12-15 00:00:00">2019/12/15</span>
        </a>
        
        <a  class="开发工具 SVN "
           href="/lrt_blog/2019/12/01/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/Windows%E7%8E%AF%E5%A2%83%E4%B8%8B%E6%90%AD%E5%BB%BASVN%E6%9C%8D%E5%8A%A1%E5%99%A8/"
           data-tag="软件安装,svn"
           data-author="" >
            <span class="post-title" title="Windows环境下搭建SVN服务器">Windows环境下搭建SVN服务器</span>
            <span class="post-date" title="2019-12-01 00:00:00">2019/12/01</span>
        </a>
        
        <a  class="开发工具 Git "
           href="/lrt_blog/2019/11/30/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/git%E9%97%AE%E9%A2%98%E9%9B%86%E9%94%A6/"
           data-tag="问题集锦,git"
           data-author="" >
            <span class="post-title" title="git问题集锦">git问题集锦</span>
            <span class="post-date" title="2019-11-30 00:00:00">2019/11/30</span>
        </a>
        
        <a  class="开发工具 Git "
           href="/lrt_blog/2019/11/30/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/Git%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"
           data-tag="git"
           data-author="" >
            <span class="post-title" title="Git 学习笔记">Git 学习笔记</span>
            <span class="post-date" title="2019-11-30 00:00:00">2019/11/30</span>
        </a>
        
        <a  class="开发工具 jd-gui "
           href="/lrt_blog/2019/11/30/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/jd-gui%E5%8F%8D%E7%BC%96%E8%AF%91jar%E5%90%8E%E6%B8%85%E9%99%A4%E5%90%8E%E9%9D%A2%E7%9A%84%E6%B3%A8%E9%87%8A%E4%BF%A1%E6%81%AF%E5%8F%8A%E8%A1%8C%E5%8F%B7/"
           data-tag="jd-gui,java,反编译"
           data-author="" >
            <span class="post-title" title="jd-gui反编译jar后清除后面的注释信息及行号">jd-gui反编译jar后清除后面的注释信息及行号</span>
            <span class="post-date" title="2019-11-30 00:00:00">2019/11/30</span>
        </a>
        
        <a  class="开发工具 Maven "
           href="/lrt_blog/2019/11/30/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/Maven%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"
           data-tag="教程,Maven"
           data-author="" >
            <span class="post-title" title="Maven 学习笔记">Maven 学习笔记</span>
            <span class="post-date" title="2019-11-30 00:00:00">2019/11/30</span>
        </a>
        
        <a  class="Windows "
           href="/lrt_blog/2019/11/24/01Windows/Windows10%E4%BC%98%E5%8C%96%E9%85%8D%E7%BD%AE/"
           data-tag="windows,系统优化"
           data-author="" >
            <span class="post-title" title="Windows10优化配置">Windows10优化配置</span>
            <span class="post-date" title="2019-11-24 00:00:00">2019/11/24</span>
        </a>
        
        <a  class="Windows "
           href="/lrt_blog/2019/11/24/01Windows/Windows10%E5%8F%96%E6%B6%88%E8%87%AA%E5%8A%A8%E6%9B%B4%E6%96%B0/"
           data-tag="windows,系统优化"
           data-author="" >
            <span class="post-title" title="Windows10取消自动更新">Windows10取消自动更新</span>
            <span class="post-date" title="2019-11-24 00:00:00">2019/11/24</span>
        </a>
        
        <a  class="Linux CentOS "
           href="/lrt_blog/2019/11/23/01Linux/CentOS%20yum%E5%91%BD%E4%BB%A4/"
           data-tag="教程,centos"
           data-author="" >
            <span class="post-title" title="yum命令">yum命令</span>
            <span class="post-date" title="2019-11-23 00:00:00">2019/11/23</span>
        </a>
        
        <a  class="Linux Ubuntu "
           href="/lrt_blog/2019/11/23/01Linux/sudo%E7%9B%B8%E5%85%B3%E6%93%8D%E4%BD%9C/"
           data-tag="教程,linux"
           data-author="" >
            <span class="post-title" title="sudo相关操作">sudo相关操作</span>
            <span class="post-date" title="2019-11-23 00:00:00">2019/11/23</span>
        </a>
        
        <a  class="Linux CentOS "
           href="/lrt_blog/2019/11/23/01Linux/CentOS%20%E6%97%B6%E5%8C%BA%E9%97%AE%E9%A2%98/"
           data-tag="centos,问题集锦"
           data-author="" >
            <span class="post-title" title="CentOS时区问题">CentOS时区问题</span>
            <span class="post-date" title="2019-11-23 00:00:00">2019/11/23</span>
        </a>
        
        <a  class="Linux "
           href="/lrt_blog/2019/11/23/01Linux/Linux%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/"
           data-tag="教程,linux"
           data-author="" >
            <span class="post-title" title="Linux常用命令">Linux常用命令</span>
            <span class="post-date" title="2019-11-23 00:00:00">2019/11/23</span>
        </a>
        
        <a  class="Linux CentOS "
           href="/lrt_blog/2019/11/23/01Linux/CentOS%20%E8%AE%BE%E7%BD%AE%E9%98%B2%E7%81%AB%E5%A2%99%E6%93%8D%E4%BD%9C%E3%80%81%E5%BC%80%E6%94%BE%E6%8C%87%E5%AE%9A%E7%AB%AF%E5%8F%A3%E6%93%8D%E4%BD%9C/"
           data-tag="centos,防火墙"
           data-author="" >
            <span class="post-title" title="CentOS设置防火墙操作、开放指定端口操作">CentOS设置防火墙操作、开放指定端口操作</span>
            <span class="post-date" title="2019-11-23 00:00:00">2019/11/23</span>
        </a>
        
        <a  class="Linux Ubuntu "
           href="/lrt_blog/2019/11/23/01Linux/ubuntu%20root%E7%94%A8%E6%88%B7%E5%91%BD%E4%BB%A4%E8%A1%A5%E5%85%A8/"
           data-tag="ubuntu"
           data-author="" >
            <span class="post-title" title="ubuntu root用户命令补全">ubuntu root用户命令补全</span>
            <span class="post-date" title="2019-11-23 00:00:00">2019/11/23</span>
        </a>
        
        <a  class="Linux "
           href="/lrt_blog/2019/11/17/01Linux/%E9%85%8D%E7%BD%AEip+yum%E6%88%96apt%E6%BA%90/"
           data-tag="教程,linux"
           data-author="" >
            <span class="post-title" title="配置ip+yum或apt源">配置ip+yum或apt源</span>
            <span class="post-date" title="2019-11-17 00:00:00">2019/11/17</span>
        </a>
        
        <a  class="Linux "
           href="/lrt_blog/2019/11/16/01Linux/%E8%99%9A%E6%8B%9F%E6%9C%BA%E4%B8%AD%E5%AE%89%E8%A3%85centos7.6%E6%95%99%E7%A8%8B/"
           data-tag="centos,软件安装"
           data-author="" >
            <span class="post-title" title="虚拟机中安装centos7.6教程">虚拟机中安装centos7.6教程</span>
            <span class="post-date" title="2019-11-16 00:00:00">2019/11/16</span>
        </a>
        
        <a  class="Linux "
           href="/lrt_blog/2019/11/16/01Linux/%E8%99%9A%E6%8B%9F%E6%9C%BA%E4%B8%AD%E5%AE%89%E8%A3%85ubuntu16.04.06%E6%95%99%E7%A8%8B/"
           data-tag="软件安装,ubuntu"
           data-author="" >
            <span class="post-title" title="虚拟机中安装ubuntu16.04.06教程">虚拟机中安装ubuntu16.04.06教程</span>
            <span class="post-date" title="2019-11-16 00:00:00">2019/11/16</span>
        </a>
        
        <a  class="工具 hexo "
           href="/lrt_blog/2019/11/14/77%E5%85%B6%E4%BB%96%E5%B7%A5%E5%85%B7/%E4%BD%BF%E7%94%A8Gitee+Hexo%E6%90%AD%E5%BB%BA%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2/"
           data-tag="教程,hexo"
           data-author="" >
            <span class="post-title" title="使用Gitee+Hexo搭建个人博客">使用Gitee+Hexo搭建个人博客</span>
            <span class="post-date" title="2019-11-14 00:00:00">2019/11/14</span>
        </a>
        
        <a  class="开发工具 Markdown "
           href="/lrt_blog/2019/11/11/77%E5%85%B6%E4%BB%96%E5%B7%A5%E5%85%B7/Markdown%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"
           data-tag="教程,Markdown"
           data-author="" >
            <span class="post-title" title="Markdown 学习笔记">Markdown 学习笔记</span>
            <span class="post-date" title="2019-11-11 00:00:00">2019/11/11</span>
        </a>
        
    </nav>
</div>
    </div>
    <div class="hide-list">
        <div class="semicircle">
            <div class="brackets first"><</div>
            <div class="brackets">&gt;</div>
        </div>
    </div>
</aside>
<div class="post">
    <div class="pjax">
        <article id="post-04存储/Linux下安装MongoDB服务" class="article article-type-post" itemscope itemprop="blogPost">
    
        <h1 class="article-title">Linux下安装MongoDB服务</h1>
    
    <div class="article-meta">
        
        
        
        <span class="book">
            
                <a data-rel="存储">存储</a>/
            
                <a data-rel="MongoDB">MongoDB</a>
            
        </span>
        
        
        <span class="tag">
            
            <a class="color5">软件安装</a>
            
            <a class="color3">MongoDB</a>
            
        </span>
        
    </div>
    <div class="article-meta">
        
        创建时间:<time class="date" title='更新时间: 2022-11-20 17:07:48'>2020-04-30 00:00</time>
        
    </div>
    <div class="article-meta">
        
        <span>字数:2.2k</span>
        
        
        <span id="busuanzi_container_page_pv">
            阅读:<span id="busuanzi_value_page_pv">
                <span class="count-comment">
                    <span class="spinner">
                      <div class="cube1"></div>
                      <div class="cube2"></div>
                    </span>
                </span>
            </span>
        </span>
        
        
        <span class="top-comment" title="跳转至评论区">
            <a href="#comments">
                评论:<span class="count-comment">
                    <span class="spinner">
                      <div class="cube1"></div>
                      <div class="cube2"></div>
                    </span>
                </span>
            </a>
        </span>
        
    </div>
    
    <div class="toc-ref">
    
        <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Linux下安装MongoDB服务"><span class="toc-text">Linux下安装MongoDB服务</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#下载"><span class="toc-text">下载</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#将安装包传到服务器"><span class="toc-text">将安装包传到服务器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#解压"><span class="toc-text">解压</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#改名"><span class="toc-text">改名</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#创建配置文件"><span class="toc-text">创建配置文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#创建数据文件夹"><span class="toc-text">创建数据文件夹</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#启动服务"><span class="toc-text">启动服务</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#关闭服务"><span class="toc-text">关闭服务</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#防火墙开放端口"><span class="toc-text">防火墙开放端口</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#集群搭建"><span class="toc-text">集群搭建</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#相关概念"><span class="toc-text">相关概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#服务器分配"><span class="toc-text">服务器分配</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#一、MongoDB-安装"><span class="toc-text">一、MongoDB 安装</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-1-下载"><span class="toc-text">1.1. 下载</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-2-建立文件夹"><span class="toc-text">1.2. 建立文件夹</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-环境变量"><span class="toc-text">1.3. 环境变量</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#二、config-server-配置"><span class="toc-text">二、config server 配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#三、shard-server-配置"><span class="toc-text">三、shard server 配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#四、mongos-配置"><span class="toc-text">四、mongos 配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#五、测试"><span class="toc-text">五、测试</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#六、在-Spring-Boot-中使用"><span class="toc-text">六、在 Spring Boot 中使用</span></a></li></ol></li></ol></li></ol>
    
<style>
    .left-col .switch-btn,
    .left-col .switch-area {
        display: none;
    }
    .toc-level-3 i,
    .toc-level-3 ol {
        display: none !important;
    }
</style>
</div>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p><div class='inner-toc'><h2>目录</h2><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Linux下安装MongoDB服务"><span class="toc-text">Linux下安装MongoDB服务</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#下载"><span class="toc-text">下载</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#将安装包传到服务器"><span class="toc-text">将安装包传到服务器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#解压"><span class="toc-text">解压</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#改名"><span class="toc-text">改名</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#创建配置文件"><span class="toc-text">创建配置文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#创建数据文件夹"><span class="toc-text">创建数据文件夹</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#启动服务"><span class="toc-text">启动服务</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#关闭服务"><span class="toc-text">关闭服务</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#防火墙开放端口"><span class="toc-text">防火墙开放端口</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#集群搭建"><span class="toc-text">集群搭建</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#相关概念"><span class="toc-text">相关概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#服务器分配"><span class="toc-text">服务器分配</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#一、MongoDB-安装"><span class="toc-text">一、MongoDB 安装</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-1-下载"><span class="toc-text">1.1. 下载</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-2-建立文件夹"><span class="toc-text">1.2. 建立文件夹</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-环境变量"><span class="toc-text">1.3. 环境变量</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#二、config-server-配置"><span class="toc-text">二、config server 配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#三、shard-server-配置"><span class="toc-text">三、shard server 配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#四、mongos-配置"><span class="toc-text">四、mongos 配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#五、测试"><span class="toc-text">五、测试</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#六、在-Spring-Boot-中使用"><span class="toc-text">六、在 Spring Boot 中使用</span></a></li></ol></li></ol></li></ol></div></p>
<h1 id="Linux下安装MongoDB服务"><a href="#Linux下安装MongoDB服务" class="headerlink" title="Linux下安装MongoDB服务"></a>Linux下安装MongoDB服务</h1><h2 id="下载"><a href="#下载" class="headerlink" title="下载"></a>下载</h2><p>从<a href="https://www.mongodb.com/download-center/community" target="_blank" rel="noopener">官网</a>下载<a href="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.6.tgz" target="_blank" rel="noopener">最新稳定版</a></p>
<h2 id="将安装包传到服务器"><a href="#将安装包传到服务器" class="headerlink" title="将安装包传到服务器"></a>将安装包传到服务器</h2><p>用winscp上传到服务器中</p>
<h2 id="解压"><a href="#解压" class="headerlink" title="解压"></a>解压</h2><pre><code class="bash">$ tar -xvf mongodb-linux-x86_64-rhel70-4.2.6.tgz</code></pre>
<h2 id="改名"><a href="#改名" class="headerlink" title="改名"></a>改名</h2><pre><code class="bash">$ mv mongodb-linux-x86_64-rhel70-4.2.6 mongodb</code></pre>
<h2 id="创建配置文件"><a href="#创建配置文件" class="headerlink" title="创建配置文件"></a>创建配置文件</h2><pre><code class="bash">$ cd mongodb
$ touch mongodb.yml
$ vi mongodb.yml
#添加
systemLog:
  destination: file
  path: &#39;./data/logs/mongodb.log&#39;
  logAppend: true
  logRotate: &#39;rename&#39;
  quiet: true
storage:
  journal:
    enabled: true
  directoryPerDB: true
  dbPath: &#39;./data/db&#39;
processManagement:
  fork: true
net:
  bindIp: 0.0.0.0
  port: 27017</code></pre>
<h2 id="创建数据文件夹"><a href="#创建数据文件夹" class="headerlink" title="创建数据文件夹"></a>创建数据文件夹</h2><pre><code class="bash">$ mkdir -p data/db data/logs</code></pre>
<h2 id="启动服务"><a href="#启动服务" class="headerlink" title="启动服务"></a>启动服务</h2><pre><code class="bash">$ ./mongod -f mongodb.yml</code></pre>
<h2 id="关闭服务"><a href="#关闭服务" class="headerlink" title="关闭服务"></a>关闭服务</h2><pre><code class="bash">$ ./mongod -f mongodb.yml --shutdown</code></pre>
<h2 id="防火墙开放端口"><a href="#防火墙开放端口" class="headerlink" title="防火墙开放端口"></a>防火墙开放端口</h2><pre><code class="bash"># 添加6379端口对外开放
$ sudo firewall-cmd --zone=public --add-port=27017/tcp --permanent
# 重新加载
$ sudo firewall-cmd --reload</code></pre>
<h2 id="集群搭建"><a href="#集群搭建" class="headerlink" title="集群搭建"></a>集群搭建</h2><p>参考<a href="http://www.ityouknow.com/mongodb/2017/08/05/mongodb-cluster-setup.html" target="_blank" rel="noopener">mongodb 3.4 集群搭建：分片+副本集</a></p>
<h3 id="相关概念"><a href="#相关概念" class="headerlink" title="相关概念"></a>相关概念</h3><p>mongodb集群有四个组件：mongos、config server、shard、replica set。</p>
<p>mongos，数据库集群请求的入口，所有的请求都通过mongos进行协调，不需要在应用程序添加一个路由选择器，mongos自己就是一个请求分发中心，它负责把对应的数据请求请求转发到对应的shard服务器上。在生产环境通常有多mongos作为请求的入口，防止其中一个挂掉所有的mongodb请求都没有办法操作。</p>
<p>config server，顾名思义为配置服务器，存储所有数据库元信息（路由、分片）的配置。mongos本身没有物理存储分片服务器和数据路由信息，只是缓存在内存里，配置服务器则实际存储这些数据。mongos第一次启动或者关掉重启就会从 config server 加载配置信息，以后如果配置服务器信息变化会通知到所有的 mongos 更新自己的状态，这样 mongos 就能继续准确路由。在生产环境通常有多个 config server 配置服务器，因为它存储了分片路由的元数据，防止数据丢失！</p>
<p>shard，分片（sharding）是指将数据库拆分，将其分散在不同的机器上的过程。将数据分散到不同的机器上，不需要功能强大的服务器就可以存储更多的数据和处理更大的负载。基本思想就是将集合切成小块，这些块分散到若干片里，每个片只负责总数据的一部分，最后通过一个均衡器来对各个分片进行均衡（数据迁移）。</p>
<p>replica set，中文翻译副本集，其实就是shard的备份，防止shard挂掉之后数据丢失。复制提供了数据的冗余备份，并在多个服务器上存储数据副本，提高了数据的可用性， 并可以保证数据的安全性。</p>
<p>仲裁者（Arbiter），是复制集中的一个MongoDB实例，它并不保存数据。仲裁节点使用最小的资源并且不要求硬件设备，不能将Arbiter部署在同一个数据集节点中，可以部署在其他应用服务器或者监视服务器中，也可部署在单独的虚拟机中。为了确保复制集中有奇数的投票成员（包括primary），需要添加仲裁节点做为投票，否则primary不能运行时不会自动切换primary。</p>
<p>简单了解之后，我们可以这样总结一下，应用请求mongos来操作mongodb的增删改查，配置服务器存储数据库元信息，并且和mongos做同步，数据最终存入在shard（分片）上，为了防止数据丢失同步在副本集中存储了一份，仲裁在数据存储到分片的时候决定存储到哪个节点。</p>
<h3 id="服务器分配"><a href="#服务器分配" class="headerlink" title="服务器分配"></a>服务器分配</h3><table>
<thead>
<tr>
<th>服务器 102</th>
<th>服务器 103</th>
<th>服务器 104</th>
</tr>
</thead>
<tbody><tr>
<td>mongos</td>
<td>mongos</td>
<td>mongos</td>
</tr>
<tr>
<td>config server</td>
<td>config server</td>
<td>config server</td>
</tr>
<tr>
<td>shard server 1 主节点</td>
<td>shard server 1 副节点</td>
<td>shard server 1 仲裁</td>
</tr>
<tr>
<td>shard server 2 仲裁</td>
<td>shard server2 主节点</td>
<td>shard server 2 副节点</td>
</tr>
<tr>
<td>shard server 3 副节点</td>
<td>shard server 3 仲裁</td>
<td>shard server 3 主节点</td>
</tr>
</tbody></table>
<p>端口：</p>
<pre><code>mongos：20000
config：21000
shard1：27001
shard2：27002
shard3：27003</code></pre><h3 id="一、MongoDB-安装"><a href="#一、MongoDB-安装" class="headerlink" title="一、MongoDB 安装"></a>一、MongoDB 安装</h3><p><strong>在 3 台服务器分别操作</strong></p>
<h4 id="1-1-下载"><a href="#1-1-下载" class="headerlink" title="1.1. 下载"></a>1.1. 下载</h4><pre><code>cd /usr/local
wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel70-v4.0-latest.tgz



tar -xzvf mongodb-linux-x86_64-rhel70-v4.0-latest.tgz -C /usr/local/
mv mongodb-linux-x86_64-rhel70-4.0.12-rc0-3-gc57d7cb mongodb</code></pre><h4 id="1-2-建立文件夹"><a href="#1-2-建立文件夹" class="headerlink" title="1.2. 建立文件夹"></a>1.2. 建立文件夹</h4><pre><code>mkdir -p /usr/local/mongodb/conf
mkdir -p /data/mongodb/mongos/log
mkdir -p /data/mongodb/config/data
mkdir -p /data/mongodb/config/log
mkdir -p /data/mongodb/shard1/data
mkdir -p /data/mongodb/shard1/log
mkdir -p /data/mongodb/shard2/data
mkdir -p /data/mongodb/shard2/log
mkdir -p /data/mongodb/shard3/data
mkdir -p /data/mongodb/shard3/log</code></pre><h4 id="1-3-环境变量"><a href="#1-3-环境变量" class="headerlink" title="1.3. 环境变量"></a>1.3. 环境变量</h4><pre><code>vim /etc/profile</code></pre><p>在文件末尾添加：</p>
<pre><code>export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH</code></pre><p>使配置立即生效：</p>
<pre><code>source /etc/profile</code></pre><h3 id="二、config-server-配置"><a href="#二、config-server-配置" class="headerlink" title="二、config server 配置"></a>二、config server 配置</h3><p><strong>在 3 台服务器分别操作</strong></p>
<pre><code>vim /usr/local/mongodb/conf/config.conf</code></pre><p>内容：</p>
<pre><code># where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/config/log/congigsrv.log



# Where and how to store data.
storage:
  dbPath: /data/mongodb/config/data
  journal:
    enabled: true



# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongodb/config/log/configsrv.pid
  timeZoneInfo: /usr/share/zoneinfo



# network interfaces
net:
  port: 21000
  bindIp: 0.0.0.0



# sharding Options
sharding:
  clusterRole: configsvr
replication:
  replSetName: config</code></pre><p>启动服务：</p>
<pre><code>mongod -f /usr/local/mongodb/conf/config.conf</code></pre><p><strong>以下操作在任意一台服务器操作即可</strong></p>
<pre><code>mongo --port 21000
# config 变量
config = {
    _id : &quot;config&quot;,
    members : [
        {_id: 0, host: &quot;192.168.30.102:21000&quot; },
        {_id: 1, host: &quot;192.168.30.103:21000&quot; },
        {_id: 2, host: &quot;192.168.30.104:21000&quot; }
    ]
}



# 初始化
rs.initiate(config)</code></pre><h3 id="三、shard-server-配置"><a href="#三、shard-server-配置" class="headerlink" title="三、shard server 配置"></a>三、shard server 配置</h3><p><strong>在 3 台服务器分别操作</strong></p>
<pre><code>vim /usr/local/mongodb/conf/shard1.conf</code></pre><p>内容：</p>
<pre><code># where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/shard1/log/shard1.log



# Where and how to store data.
storage:
  dbPath: /data/mongodb/shard1/data
  journal:
    enabled: true



# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongodb/shard1/log/shard1.pid
  timeZoneInfo: /usr/share/zoneinfo



# network interfaces
net:
  port: 27001
  bindIp: 0.0.0.0



# sharding Options
sharding:
  clusterRole: shardsvr
replication:
  replSetName: shard1</code></pre><p>启动服务：</p>
<pre><code>mongod -f /usr/local/mongodb/conf/shard1.conf</code></pre><p><strong>以下操作在任意一台服务器操作即可 (实际操作中需要在非裁判服务器操作)</strong></p>
<pre><code>mongo --port 27001
use admin



# &quot;arbiterOnly&quot;:true 代表其为仲裁节点
config = {
    _id: &quot;shard1&quot;,
    members: [
        {_id: 0, host: &quot;192.168.30.102:27001&quot;},
        {_id: 1, host: &quot;192.168.30.103:27001&quot;},
        {_id: 2, host: &quot;192.168.30.104:27001&quot;, arbiterOnly: true}
    ]
}
# 初始化
rs.initiate(config)</code></pre><p><strong>重复上述操作配置 shard2 和 shard3, 注意修改名称、端口和对应的 arbiterOnly</strong></p>
<h3 id="四、mongos-配置"><a href="#四、mongos-配置" class="headerlink" title="四、mongos 配置"></a>四、mongos 配置</h3><p><strong>在 3 台服务器分别操作</strong></p>
<pre><code>vim /usr/local/mongodb/conf/mongos.conf</code></pre><p>内容：</p>
<pre><code># where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/mongos/log/mongos.log



# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongodb/mongos/log/mongos.pid
  timeZoneInfo: /usr/share/zoneinfo



# network interfaces
net:
  port: 20000
  bindIp: 0.0.0.0



# sharding Options
# config 为配置服务器的副本集名字
sharding:
  configDB: config/192.168.30.102:21000,192.168.30.103:21000,192.168.30.104:21000</code></pre><p>启动服务:</p>
<pre><code>mongos -f /usr/local/mongodb/conf/mongos.conf</code></pre><p><strong>以下操作在任意一台服务器操作即可</strong></p>
<pre><code>mongo --port 20000
use admin



# 串联路由服务器与分配副本集

sh.addShard(&quot;shard1/192.168.30.102:27001,192.168.30.103:27001,192.168.30.104:27001&quot;)
sh.addShard(&quot;shard2/192.168.30.102:27002,192.168.30.103:27002,192.168.30.104:27002&quot;)
sh.addShard(&quot;shard3/192.168.30.102:27003,192.168.30.103:27003,192.168.30.104:27003&quot;)
# 注：如果出错只保留主服务器ip</code></pre><h3 id="五、测试"><a href="#五、测试" class="headerlink" title="五、测试"></a>五、测试</h3><p>在其中一台 mongos 继续操作：</p>
<pre><code>use admin
# 指定 test 数据库分片生效
db.runCommand({ enablesharding :&quot;test&quot;})

# 指定数据库里需要分片的集合和片键
db.runCommand({ shardcollection : &quot;test.table1&quot;,key : {id: 1}})

use  test;
# 插入测试数据
for(var i = 1; i &lt;= 100000; i++) db.table1.save({id:i,&quot;test1&quot;:&quot;testval1&quot;})

#查看分片情况如下，部分无关信息省掉了
db.table1.stats();</code></pre><p>我们设置testdb的 table1 表需要分片，根据 id 自动分片到 shard1 ，shard2，shard3 上面去。要这样设置是因为不是所有mongodb 的数据库和表 都需要分片！</p>
<pre><code>{
        &quot;sharded&quot; : true,
        &quot;ns&quot; : &quot;testdb.table1&quot;,
        &quot;count&quot; : 100000,
        &quot;numExtents&quot; : 13,
        &quot;size&quot; : 5600000,
        &quot;storageSize&quot; : 22372352,
        &quot;totalIndexSize&quot; : 6213760,
        &quot;indexSizes&quot; : {
                &quot;_id_&quot; : 3335808,
                &quot;id_1&quot; : 2877952
        },
        &quot;avgObjSize&quot; : 56,
        &quot;nindexes&quot; : 2,
        &quot;nchunks&quot; : 3,
        &quot;shards&quot; : {
                &quot;shard1&quot; : {
                        &quot;ns&quot; : &quot;testdb.table1&quot;,
                        &quot;count&quot; : 42183,
                        &quot;size&quot; : 0,
                        ...
                        &quot;ok&quot; : 1
                },
                &quot;shard2&quot; : {
                        &quot;ns&quot; : &quot;testdb.table1&quot;,
                        &quot;count&quot; : 38937,
                        &quot;size&quot; : 2180472,
                        ...
                        &quot;ok&quot; : 1
                },
                &quot;shard3&quot; : {
                        &quot;ns&quot; : &quot;testdb.table1&quot;,
                        &quot;count&quot; :18880,
                        &quot;size&quot; : 3419528,
                        ...
                        &quot;ok&quot; : 1
                }
        },
        &quot;ok&quot; : 1
}</code></pre><p>可以看到数据分到3个分片，各自分片数量为： shard1 “count” : 42183，shard2 “count” : 38937，shard3 “count” : 18880。已经成功了！</p>
<h3 id="六、在-Spring-Boot-中使用"><a href="#六、在-Spring-Boot-中使用" class="headerlink" title="六、在 Spring Boot 中使用"></a>六、在 Spring Boot 中使用</h3><p>在其中一台 mongos 继续操作：</p>
<pre><code># 创建账户
db.createUser({
 user: &#39;test&#39;,
 pwd: &#39;123456&#39;,
 roles: [{role: &quot;readWrite&quot;, db: &quot;test&quot;}]
})</code></pre><p>Spring Boot 连接字符串：</p>
<pre><code>spring:
  data:
    mongodb:
      uri: mongodb://test:123456@192.168.30.102:20000,192.168.30.103:20000,192.168.30.104:20000/test</code></pre>
      
       <hr><span style="font-style: italic;color: gray;"> 转载请注明来源，欢迎对文章中的引用来源进行考证，欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论，也可以邮件至 86415270@qq.com </span>
    </div>
</article>


<p>
    <a class="dashang" onclick="dashangToggle()">赏</a>
</p>


<div class="article_copyright">
    <p><span class="copy-title">文章标题:</span>Linux下安装MongoDB服务</p>
    <p><span class="copy-title">文章字数:</span><span class="post-count">2.3k</span></p>
    <p><span class="copy-title">本文作者:</span><a title="李瑞桐">李瑞桐</a></p>
    <p><span class="copy-title">发布时间:</span>2020-04-30, 00:00:00</p>
    <p><span class="copy-title">最后更新:</span>2022-11-20, 17:07:48</p>
    <span class="copy-title">原始链接:</span><a class="post-url" href="/lrt_blog/2020/04/30/04%E5%AD%98%E5%82%A8/Linux%E4%B8%8B%E5%AE%89%E8%A3%85MongoDB%E6%9C%8D%E5%8A%A1/" title="Linux下安装MongoDB服务">https://loveyangxiaoyu.gitee.io/lrt_blog/2020/04/30/04%E5%AD%98%E5%82%A8/Linux%E4%B8%8B%E5%AE%89%E8%A3%85MongoDB%E6%9C%8D%E5%8A%A1/</a>
    <p>
        <span class="copy-title">版权声明:</span><i class="fa fa-creative-commons"></i> <a rel="license noopener" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
    </p>
</div>



    <div id="comments"></div>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">

<script type="text/javascript">
    $.getScript('/lrt_blog/js/gitalk.js', function () {
        var gitalk = new Gitalk({
            clientID: '977658d53bffc49a7231',
            clientSecret: 'bd5ef398e64a580d122200d984d56f6da067ff83',
            repo: 'lrt890424.github.io',
            owner: 'lrt890424',
            admin: ['lrt890424'],
            id: decodeURI(location.pathname),
            distractionFreeMode: 'true',
            language: 'zh-CN',
            perPage: parseInt('10',10)
        })
        gitalk.render('comments')
    })
</script>




    




    </div>
    <div class="copyright">
        <p class="footer-entry">©2016-2020 Li Ruitong</p>
<p class="footer-entry">Built with <a href="https://hexo.io/" target="_blank">Hexo</a> and <a href="https://github.com/yelog/hexo-theme-3-hexo" target="_blank">3-hexo</a> theme</p>

    </div>
    <div class="full-toc">
        <button class="full"><span class="min "></span></button>
<button class="post-toc-menu"><span class="post-toc-menu-icons"></span></button>
<div class="post-toc"><span class="post-toc-title">目录</span>
    <div class="post-toc-content">

    </div>
</div>
<a class="" id="rocket" ></a>
    </div>
</div>
<div class="acParent"></div>

<div class="hide_box" onclick="dashangToggle()"></div>
<div class="shang_box">
    <a class="shang_close" onclick="dashangToggle()">×</a>
    <div class="shang_tit">
        <p>喜欢就点赞,疼爱就打赏</p>
    </div>
    <div class="shang_payimg">
        <div class="pay_img">
            <img src="/lrt_blog/img/alipay.jpg" class="alipay" title="扫码支持">
            <img src="/lrt_blog/img/weixin.jpg" class="weixin" title="扫码支持">
        </div>
    </div>
    <div class="shang_payselect">
        <span><label><input type="radio" name="pay" checked value="alipay">支付宝</label></span><span><label><input type="radio" name="pay" value="weixin">微信</label></span>
    </div>
</div>


</body>
<script src="/lrt_blog/js/jquery.pjax.js?v=1.0.0" ></script>

<script src="/lrt_blog/js/script.js?v=1.0.0" ></script>
<!-- 代码块复制功能 -->
<script type="text/javascript" src="/lrt_blog/js/clipboard.min.js"></script>
<script type="text/javascript" src="/lrt_blog/js/clipboard-use.js"></script>
<!-- mermaid chart -->

<script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
<script>
    if (window.mermaid) {
        mermaid.initialize({theme: 'forest'});
    }
</script>

<script>
    var img_resize = 'default';
    /*作者、标签的自动补全*/
    $(function () {
        $('.search').AutoComplete({
            'data': ['#教程','#centos','#linux','#问题集锦','#防火墙','#软件安装','#ubuntu','#svn','#git','#MongoDB','#jd-gui','#java','#反编译','#MySQL','#Redis','#学习笔记','#navicat','#DM','#windows','#系统优化','#书签','#Nginx','#docker','#vue','#Yarn','#前端开发','#vscode','#eslint','#prettier','#Markdown','#hexo','#开源','#Maven',],
            'itemHeight': 20,
            'width': 418
        }).AutoComplete('show');
    })
    function initArticle() {
        /*渲染对应的表格样式*/
        
            $(".post .pjax table").addClass("green_title");
        

        /*渲染打赏样式*/
        
        $("input[name=pay]").on("click", function () {
            if($("input[name=pay]:checked").val()=="weixin"){
                $(".shang_box .shang_payimg .pay_img").addClass("weixin_img");
            } else {
                $(".shang_box .shang_payimg .pay_img").removeClass("weixin_img");
            }
        })
        

        /*高亮代码块行号*/
        
        $('pre code').each(function(){
            var lines = $(this).text().split('\n').length, widther='';
            if (lines>99) {
                widther = 'widther'
            }
            var $numbering = $('<ul/>').addClass('pre-numbering ' + widther).attr("unselectable","on");
            $(this).addClass('has-numbering ' + widther)
                    .parent()
                    .append($numbering);
            for(var i=1;i<=lines;i++){
                $numbering.append($('<li/>').text(i));
            }
        });
        

        /*访问数量*/
        
        $.getScript("//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js");
        

        /*代码高亮，行号对齐*/
        $('.pre-numbering').css('line-height',$('.has-numbering').css('line-height'));

        
    }

    /*打赏页面隐藏与展示*/
    
    function dashangToggle() {
        $(".shang_box").fadeToggle();
        $(".hide_box").fadeToggle();
    }
    

</script>

<!--加入行号的高亮代码块样式-->

<style>
    pre{
        position: relative;
        margin-bottom: 24px;
        border-radius: 10px;
        border: 1px solid #e2dede;
        background: #FFF;
        overflow: hidden;
    }
    code.has-numbering{
        margin-left: 30px;
    }
    code.has-numbering.widther{
        margin-left: 35px;
    }
    .pre-numbering{
        margin: 0px;
        position: absolute;
        top: 0;
        left: 0;
        width: 20px;
        padding: 0.5em 3px 0.7em 5px;
        border-right: 1px solid #C3CCD0;
        text-align: right;
        color: #AAA;
        background-color: #fafafa;
    }
    .pre-numbering.widther {
        width: 35px;
    }
</style>

<!--自定义样式设置-->
<style>
    
    
    .nav {
        width: 542px;
    }
    .nav.fullscreen {
        margin-left: -542px;
    }
    .nav-left {
        width: 120px;
    }
    
    
    @media screen and (max-width: 1468px) {
        .nav {
            width: 492px;
        }
        .nav.fullscreen {
            margin-left: -492px;
        }
        .nav-left {
            width: 100px;
        }
    }
    
    
    @media screen and (max-width: 1024px) {
        .nav {
            width: 492px;
            margin-left: -492px
        }
        .nav.fullscreen {
            margin-left: 0;
        }
        .nav .hide-list.fullscreen {
            left: 492px
        }
    }
    
    @media screen and (max-width: 426px) {
        .nav {
            width: 100%;
        }
        .nav-left {
            width: 100%;
        }
    }
    
    
    .nav-right .title-list nav a .post-title, .nav-right .title-list #local-search-result a .post-title {
        color: #383636;
    }
    
    
    .nav-right .title-list nav a .post-date, .nav-right .title-list #local-search-result a .post-date {
        color: #5e5e5f;
    }
    
    
    .nav-right nav a.hover, #local-search-result a.hover{
        background-color: #e2e0e0;
    }
    
    

    /*列表样式*/
    
    .post .pjax article .article-entry>ol, .post .pjax article .article-entry>ul, .post .pjax article>ol, .post .pjax article>ul{
        border: #e2dede solid 1px;
        border-radius: 10px;
        padding: 10px 32px 10px 56px;
    }
    .post .pjax article .article-entry li>ol, .post .pjax article .article-entry li>ul,.post .pjax article li>ol, .post .pjax article li>ul{
        padding-top: 5px;
        padding-bottom: 5px;
    }
    .post .pjax article .article-entry>ol>li, .post .pjax article .article-entry>ul>li,.post .pjax article>ol>li, .post .pjax article>ul>li{
        margin-bottom: auto;
        margin-left: auto;
    }
    .post .pjax article .article-entry li>ol>li, .post .pjax article .article-entry li>ul>li,.post .pjax article li>ol>li, .post .pjax article li>ul>li{
        margin-bottom: auto;
        margin-left: auto;
    }
    

    /* 背景图样式 */
    
    


    /*引用块样式*/
    

    /*文章列表背景图*/
    

    
</style>







</html>
